#include "FarmerCard.h"
#include "ui_FarmerCard.h"
#include "Algorithm.h"

FarmerCard::FarmerCard(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FarmerCard)
{
    ui->setupUi(this);
}

FarmerCard::~FarmerCard()
{
    delete ui;
}

void FarmerCard::ShowDuKa(QByteArray &data)
{
    QString str;

    if (data.size() < 16)
        return;

    str = Algorithm::fromBCD(data);
    ui->leDuYinHangKa->setText(str);
}

bool FarmerCard::GetSpecKeyB(QByteArray &keyb)
{
    if (ui->ZhiDingMiYue->isChecked())
    {
        keyb = Algorithm::toByte(ui->MiYue->currentText());

        return true;
    }
    //可能会修改卡号
    if (!ui->leDuYinHangKa->text().isEmpty())
    {
        QString str;

        str = ui->leDuYinHangKa->text();
        Algorithm::calcKeyB(str, keyb);

        return true;
    }

    return false;
}

void FarmerCard::GetKeyB(QByteArray &keyb)
{
    QString kahao;

    kahao = ui->leXieYinHangKa->text();

    Algorithm::calcKeyB(kahao, keyb);
}

bool FarmerCard::GetInfo(QByteArray &data, QByteArray &keyb, QByteArray &blk3, int &mode)
{
    QString kahao;
    QByteArray nkey;

    kahao = ui->leXieYinHangKa->text();

    if (kahao.length() < 16)
    {
        statustip = "请输入卡号";
        return false;
    }
    data = Algorithm::toBCD(kahao);

    GetKeyB(keyb);

    if(GetSpecKeyB(nkey)) //使用指定密钥
    {
        blk3.resize(10);
        blk3.fill(0, 10);
        blk3.data()[6] = 0x78;
        blk3.data()[7] = 0x77;
        blk3.data()[8] = 0x88;
        blk3.data()[9] = 0x00;
        blk3 += keyb;

        keyb = nkey;
    }

    if (ui->leDuYinHangKa->text().isEmpty())
    {
        mode = 3;
    }
    else
    {
        mode = 4;
    }

    return true;
}

void FarmerCard::Clear()
{
    ui->leDuYinHangKa->clear();
    ui->leXieYinHangKa->clear();
}

void FarmerCard::on_btDuKa_clicked()
{
    Clear();
    emit pbEvent(10);
}

void FarmerCard::on_btXieKa_clicked()
{
    emit pbEvent(11);
}
